package com.wwp.tbshop.model.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.NewGoodsActivity;
import com.wwp.tbshop.activity.ProductDetailActivity;
import com.wwp.tbshop.model.bean.NewGoodsBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${wenping} on 6/16/2017.
 */

public class NewGoodsRVAdapater extends RecyclerView.Adapter {

    private Context mContext;
    private NewGoodsActivity mNewGoodsActivity;

    public NewGoodsRVAdapater(Context context, NewGoodsActivity newGoodsActivity) {
        mContext = context;
        mNewGoodsActivity = newGoodsActivity;
    }

    private List<NewGoodsBean.ProductListBean> mProductListBeanList;

    public void setProductListBeanList(List<NewGoodsBean.ProductListBean> productListBeanList) {
        mProductListBeanList = productListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_new_goods, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(mProductListBeanList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mProductListBeanList!=null) {
            return mProductListBeanList.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.item_limit_buy_goodsiv)
        ImageView mItemLimitBuyGoodsiv;
        @Bind(R.id.item_limit_buy_name)
        TextView mItemLimitBuyName;
        @Bind(R.id.item_limit_buy_marketprice)
        TextView mItemLimitBuyMarketprice;
        @Bind(R.id.item_limit_buy_price)
        TextView mItemLimitBuyPrice;
        private NewGoodsBean.ProductListBean mProductListBean;

        private ViewHolder(View view)
        {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pId = mProductListBean.getId();
                    Intent intent = new Intent(mContext, ProductDetailActivity.class);
                    intent.putExtra("pId",pId);
                    mContext.startActivity(intent);
                }
            });
        }
    /**给item设置数据: 图片,价格,名字*/
        public void setData(NewGoodsBean.ProductListBean bean) {
            mProductListBean = bean;
//            mData = data;
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST+bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemLimitBuyGoodsiv);
            mItemLimitBuyName.setText(bean.getName()+"");
            mItemLimitBuyMarketprice.setText("市价:¥"+bean.getMarketPrice()+"");
            mItemLimitBuyMarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            mItemLimitBuyPrice.setTextColor(Color.RED);
            mItemLimitBuyPrice.setText("现价:¥"+bean.getPrice()+"");
        }
    }
}
